package com.ct.ah.lab.controller;

import com.ct.ah.lab.entity.Cash;
import com.ct.ah.lab.entity.User;
import com.ct.ah.lab.entity.vo.CashItemVo;
import com.ct.ah.lab.entity.vo.PointItemVo;
import com.ct.ah.lab.service.ICashService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/cash")
public class CashController {

    @Autowired
    private ICashService cashService;

    //查询用户当前现金金额
    @RequestMapping("/getCashByUid")
    @ResponseBody
    public Map<String, Object> getCashByUid(HttpServletRequest req) {
        Map<String, Object> map = new HashMap<>();

        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("loginUser");
        if (user == null) {
            map.put("msg", "not login");
        } else {
            Cash cash = cashService.getCashByUid(user.getUid());
            map.put("state", 1000);
            map.put("data", cash);
            if (cash != null) {
                map.put("msg", true);
            } else {
                map.put("msg", false);

            }
        }
        return map;
    }

    //根据用户id-查询所有的充值和消费记录
    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> list(HttpServletRequest req) {
        Map<String, Object> map = new HashMap<>();
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("loginUser");
        if (user == null) {
            map.put("msg", "not login");
        } else {
            List<CashItemVo> pointItemVos = cashService.listCashItem(user);
            map.put("state", 1000);
            map.put("data", pointItemVos);
        }
        return map;
    }

    //进行现金充值
    @RequestMapping("/chargeCash")
    @ResponseBody
    public Map<String, Object> chargeCash(Double cashMoney,Integer chargeStyle,HttpServletRequest req) {
        Map<String, Object> map = new HashMap<>();
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("loginUser");
        if (user == null) {
            map.put("msg", "not login");
        }else {
            //对当前用户进行现金充值
            cashService.chargeCash(user,cashMoney,chargeStyle);
            map.put("state", 1000);
        }
        return map;
    }


    //进行现金充值
    @RequestMapping("/cashTransfer")
    @ResponseBody
    public Map<String, Object> cashTransfer(String inUsername,Double outMoney,HttpServletRequest req) {
        Map<String, Object> map = new HashMap<>();
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("loginUser");
        if (user == null) {
            map.put("msg", "not login");
        }else {
            //对当前用户进行现金充值
            cashService.cashTransfer(user,inUsername,outMoney);
            map.put("state", 1000);
        }
        return map;
    }

    //根据一个订单保存一条积分记录
//    @RequestMapping("/addPointByOrderId")
//    @ResponseBody
//    public Map<String, Object> addPointByOrderId(Integer oid, HttpServletRequest req) {
//        Map<String, Object> map = new HashMap<>();
//        HttpSession session = req.getSession();
//        User user = (User) session.getAttribute("loginUser");
//        if (user == null) {
//            map.put("msg", "not login");
//        } else {
//            pointService.savePointItem(oid);
//            map.put("state", 1000);
//        }
//        return map;
//    }

    //使用钱包进行支付
    @RequestMapping("/payByCash")
    @ResponseBody
    public Map<String, Object> payByCash(Integer oid, HttpServletRequest req) {
        Map<String, Object> map = new HashMap<>();
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("loginUser");
        if (user == null) {
            map.put("msg", "not login");
        } else {
            cashService.payByCash(oid, user);
            map.put("state", 1000);
        }
        return map;
    }
}
